<template>
  <div class="app-container">

    
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
      <el-form-item label="batch_no" prop="batchNo">
        <el-input v-model="temp.batchNo" />
      </el-form-item>
      <el-form-item label="total_num" prop="totalNum">
        <el-input v-model="temp.totalNum" />
      </el-form-item>
      <el-form-item label="current_num" prop="currentNum">
        <el-input v-model="temp.currentNum" />
      </el-form-item>
      <el-form-item label="merchant_code" prop="merchantCode">
        <el-input v-model="temp.merchantCode" />
      </el-form-item>
      <el-form-item label="cario_id" prop="carioId">
        <el-input v-model="temp.carioId" />
      </el-form-item>
      <el-form-item label="t_info_goods_id" prop="tInfoGoodsId">
        <el-input v-model="temp.tInfoGoodsId" />
      </el-form-item>
      <el-form-item label="t_info_supplier_id" prop="tInfoSupplierId">
        <el-input v-model="temp.tInfoSupplierId" />
      </el-form-item>
      <el-form-item label="org_code" prop="orgCode">
        <el-input v-model="temp.orgCode" />
      </el-form-item>
      <el-form-item label="删除标志" prop="deleteflag">
        <el-select  v-model="temp.deleteflag" placeholder="请选择">
          <el-option
            v-for="item in deleteflagList"
            :key="item.keyName"
            :label="item.valueName"
            :value="item.keyName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="ow_offpost_event_type" prop="owOffpostEventType">
        <el-input v-model="temp.owOffpostEventType" />
      </el-form-item>

      <el-form-item v-show="!isFromFather">
        <el-button type="primary" @click="createData">
          保存
        </el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>

import {
  createApi, updateApi,
  select4enums
} from './api'

export default {
  name: 'TLogErpAdd',

  props: {
    isFromFather: {
      type: Boolean,
      default: false
    },
    row: {}
  },

  data() {
    return {
      rules: {
        batchNo: [{ required: true, message: 'required', trigger: 'blur' }],
        totalNum: [{ required: true, message: 'required', trigger: 'blur' }],
        currentNum: [{ required: true, message: 'required', trigger: 'blur' }],
        merchantCode: [{ required: true, message: 'required', trigger: 'blur' }],
        carioId: [{ required: true, message: 'required', trigger: 'blur' }],
        tInfoGoodsId: [{ required: true, message: 'required', trigger: 'blur' }],
        tInfoSupplierId: [{ required: true, message: 'required', trigger: 'blur' }],
        orgCode: [{ required: true, message: 'required', trigger: 'blur' }],
        deleteflag: [{ required: true, message: 'required', trigger: 'blur' }],
        owOffpostEventType: [{ required: true, message: 'required', trigger: 'blur' }],
      },
      temp: {},
      deleteflagList: [],
      listLoading: true
    }
  },

  watch: {
    row() {
      this.temp = Object.assign({}, this.row) // copy obj
    }
  },

  mounted() {
    this.initSelectData()
  },

  methods: {
    initSelectData() {
      // 枚举取值
      select4enums({ enumName: 'Deleteflag' }).then(resp => {
        this.deleteflagList = resp.data
        setTimeout(() => {
          this.temp.deleteflag = resp.data[0].keyName
          if (this.isFromFather) {
            this.temp = Object.assign({}, this.row)
          }
        })
      })
    },

    resetTemp() {
      this.temp = {
        batchNo: '',
        totalNum: '',
        currentNum: '',
        merchantCode: '',
        carioId: '',
        tInfoGoodsId: '',
        tInfoSupplierId: '',
        orgCode: '',
        deleteflag: '',
        owOffpostEventType: '',
        id: undefined
      }
    },

    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const param = Object.assign({}, this.temp)
          createApi(param).then((result) => {
            if (result.success) {
              this.resetTemp()
              this.$notify({
                title: '成功',
                message: '保存成功',
                type: 'success',
                duration: 2000
              })
              // 从父组件调用此方法，进行回调下
              if (this.isFromFather) {
                this.$emit('createResult')
              }
            }
        })
        }
      })
    },

    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const param = { id:this.temp.id }
          param.batchNo = this.temp.batchNo
          param.totalNum = this.temp.totalNum
          param.currentNum = this.temp.currentNum
          param.merchantCode = this.temp.merchantCode
          param.carioId = this.temp.carioId
          param.tInfoGoodsId = this.temp.tInfoGoodsId
          param.tInfoSupplierId = this.temp.tInfoSupplierId
          param.orgCode = this.temp.orgCode
          param.deleteflag = this.temp.deleteflag
          param.owOffpostEventType = this.temp.owOffpostEventType
          updateApi(param).then((result) => {
            if (result.success) {
              this.resetTemp()
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
              // 从父组件调用此方法，进行回调下
              if (this.isFromFather) {
                this.$emit('updateResult')
              }
            }
        })
        }
      })
    },

    clearValidate() {
      this.$refs['dataForm'].clearValidate()
    }


  }
}
</script>
<style>

</style>
